//
//  MineJiFenViewController.m
//  FD
//
//  Created by Yasin on 2018/10/16.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "MineJiFenViewController.h"
//h ": 60
@interface MineJiFenCell : UITableViewCell
/**标题*/
@property (nonatomic,  strong) UILabel *nameLb;
/**时间*/
@property (nonatomic,  strong) UILabel *timeLb;
/**增长量*/
@property (nonatomic,  strong) UILabel *addCountLb;
/**线*/
@property (nonatomic, strong) UILabel *lightGrayLineView;

@end

@implementation MineJiFenCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        [self setupView];
        
        //取消选中样式
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void)setupView {
    [self.contentView addSubview:self.nameLb];
    [self.contentView addSubview:self.timeLb];
    [self.contentView addSubview:self.addCountLb];
    [self.contentView addSubview:self.lightGrayLineView];
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(self.mas_top).offset(5);
        make.size.mas_equalTo(CGSizeMake(200, 25));
    }];
    [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(self.nameLb.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(300, 25));
    }];
    
    
    [self.addCountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 35));
        make.right.equalTo(self.contentView.mas_right).offset(-15);
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    
    
    //2019-2-14线
    [self.lightGrayLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(15);
        make.right.equalTo(self.contentView.mas_right).offset(-15);
        make.height.mas_equalTo(1);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-1);
    }];
}



- (UILabel *)nameLb {
    if(!_nameLb) {
        _nameLb = [UILabel new];
    }
    return _nameLb;
}
- (UILabel *)timeLb {
    if(!_timeLb) {
        _timeLb = [UILabel  new];
        _timeLb.textColor = [UIColor lightGrayColor];
    }
    return _timeLb;
}
- (UILabel *)addCountLb {
    if (!_addCountLb) {
        _addCountLb = [UILabel new];
        _addCountLb.textAlignment = NSTextAlignmentRight;
    }
    return _addCountLb;
}

- (UILabel *)lightGrayLineView {
    if (!_lightGrayLineView) {
        _lightGrayLineView = [UILabel new];
        _lightGrayLineView.backgroundColor = kMainLightGrayColor;
    }
    return _lightGrayLineView;
}

@end

@interface MineJiFenViewController ()<UITableViewDelegate,UITableViewDataSource>
/**积分数量*/
@property (nonatomic,  strong) UILabel *jiFenCountLb;
/**tableView*/
@property (nonatomic,  strong) UITableView *tv;
/**数据源*/
@property (nonatomic,  strong) NSArray *dataArr;
/**数据源*/
@property (nonatomic,  strong) NSMutableArray *dataArrM;


@end

@implementation MineJiFenViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = NSLocalizedString(My_jiFen, @"我的v积分");
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupView];
    
    [self initDataArr];
    
    [self loadJiFenData];
}

- (void)initDataArr
{
    self.dataArr = @[@{@"name":@"门店预约",@"time":@"2018-4-18 17：33",@"addCount":@"+20"},
                     @{@"name":@"外我附近",@"time":@"2018-4-18 17：33",@"addCount":@"+20"},
                     @{@"name":@"门店预约",@"time":@"2018-4-18 17：33",@"addCount":@"+20"},
                     @{@"name":@"门店预约",@"time":@"2018-4-18 17：33",@"addCount":@"+20"},
                     @{@"name":@"门店预约",@"time":@"2018-4-18 17：33",@"addCount":@"+20"},
                     @{@"name":@"门店四个",@"time":@"2018-4-18 17：33",@"addCount":@"+20"},
                     @{@"name":@"门店预约",@"time":@"2018-4-18 17：33",@"addCount":@"+20"},
                     @{@"name":@"门店预约",@"time":@"2018-4-18 17：33",@"addCount":@"+20"},
                     @{@"name":@"门店预约",@"time":@"2018-4-18 17：33",@"addCount":@"+20"},
                     @{@"name":@"门店预约",@"time":@"2018-4-18 17：33",@"addCount":@"+20"},
                     @{@"name":@"门店预约",@"time":@"2018-4-18 17：33",@"addCount":@"+20"},
                     @{@"name":@"门店预约",@"time":@"2018-4-18 17：33",@"addCount":@"+20"},
                     @{@"name":@"门店预约",@"time":@"2018-4-18 17：33",@"addCount":@"+20"},
                     ];
}
- (void)setupView {
    self.tv.frame = self.view.bounds;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    headerView.backgroundColor = [UIColor whiteColor];
    self.jiFenCountLb.frame = CGRectMake(0, 0, SCREEN_WIDTH, 150);
    [headerView addSubview:self.jiFenCountLb];
    self.tv.tableHeaderView = headerView;
    self.tv.tableFooterView = [UIView new];
    
    [self.view addSubview:self.tv];
}

//getter
- (UILabel *)jiFenCountLb {
    if (!_jiFenCountLb) {
        _jiFenCountLb = [UILabel new];
        _jiFenCountLb.textColor = [UIColor redColor];
        _jiFenCountLb.font = [UIFont systemFontOfSize:40.0];
        _jiFenCountLb.textAlignment = NSTextAlignmentCenter;
    }
    return _jiFenCountLb;
}
- (UITableView *)tv {
    if (!_tv) {
        _tv = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        //代理
        _tv.delegate = self;
        _tv.dataSource = self;
        //注册cell
        [_tv registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
        [_tv registerClass:[MineJiFenCell class] forCellReuseIdentifier:@"MineJiFenCell"];
        
        //取消线
        _tv.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tv;
}
- (NSMutableArray *)dataArrM {
    if (!_dataArr) {
        _dataArrM = [NSMutableArray array];
    }
    return _dataArrM;
}

#pragma mark - 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        //2019-2-14 有才进行显示
        if (self.dataArrM.count < 1) {
            return 0;
        }else {
            return 1;
        }
    }else {
        return self.dataArrM.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
        UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        cell1.textLabel.text = [NSString stringWithFormat:@"%@:",NSLocalizedString(L_JiFenLeiJiMingXi,@"累计明细:")];
        //添加一条灰色的线
        UIView *lightGrayLineView = [UIView new];
        lightGrayLineView.backgroundColor = kMainLightGrayColor;
        [cell1.contentView addSubview:lightGrayLineView];
        [lightGrayLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell1.contentView.mas_left).offset(15);
            make.right.equalTo(cell1.contentView.mas_right).offset(-15);
            make.bottom.equalTo(cell1.contentView.mas_bottom);
            make.height.mas_equalTo(1);
        }];
        return cell1;
    }else {
        MineJiFenCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineJiFenCell"];
        NSDictionary *dic = self.dataArrM[indexPath.row];
        NSNumber *amount = dic[@"amount"]; //
        NSNumber *type = dic[@"type"];
        NSNumber *createTime = dic[@"createTime"];
        
        
        //字段type说明：  0：外卖点餐 1：自提点餐 2：门店预约
        NSString *typeStr = @"";
        if ([type isEqualToNumber:@(0)]) {
            typeStr = NSLocalizedString(L_JiFenWaiMaiDianCan, @"外卖点餐");
        }else if ([type isEqualToNumber:@(1)]) {
            typeStr = NSLocalizedString(L_JiFenZiTiDianCan, @"自提点餐");
        }else if ([type isEqualToNumber:@(2)]) {
            
            typeStr = @"Eat In ";
            if ([NSString rb_ChineseLanguage]) {
                typeStr = @"门店预约";
            }
        }
        cell.nameLb.text = typeStr;
        
        
        cell.addCountLb.text = [NSString stringWithFormat:@"%@",amount]; //新增积分数量

        
        cell.timeLb.text = [self stringFromTimeStamp:createTime]; //时间
        
        return cell;
    }
}

//时间戳转时间字符串
- (NSString *)stringFromTimeStamp:(NSNumber *)timeStamp {
    
    FDLog(@"%@",timeStamp);

    NSTimeInterval interval = [timeStamp floatValue] / 1000;
    
    NSDate *date  = [NSDate dateWithTimeIntervalSince1970:interval];

    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *dateString  = [fmt stringFromDate:date];
    
    return dateString;
}








- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}


#pragma mark - 请求数据
- (void)loadJiFenData {
    [FDHttpAPi requestJiFenMingXiSuccessBlock:^(id responseObjc) {
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            NSDictionary *dic = responseObjc[@"data"];
            NSNumber *amount = dic[@"amount"]; //服务器返回的数字都是Number类型的
//            self.jiFenCountLb.text = [NSString stringWithFormat:@"%.2f",[amount floatValue]];
            self.jiFenCountLb.text = [NSString stringWithFormat:@"%@",amount];

            NSArray *detailList = dic[@"detailList"];
            if (detailList.count <= 0) {
                FDLog(@"空什么都不做！！！");
            }else {
                self.dataArrM = [NSMutableArray arrayWithArray:detailList];
            }
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
        
        //刷新
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tv reloadData];
        });
        
    } fialBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}


@end
